//
//  RankViewController.m
//  CookDemo
//
//  Created by lanou3g on 15/8/30.
//  Copyright (c) 2015年 DTR. All rights reserved.
//

#import "RankViewController.h"
#import "RankCollectionViewCell.h"
#import "RankingKitchenInfo.h"
#import "KitchenManager.h"
#import "XiangHaLifeInfo.h"
#import "RankCollectionViewCell.h"
#import "RankingKitchenInfo.h"
#import "XiangHaLifeInfo.h"
#import "RankDetailViewController.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"

static const CGFloat MJDuration = 2.0;
@interface RankViewController ()

@property (nonatomic, strong)NSMutableArray *data;
@end

@implementation RankViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.collctionView reloadData];
   
    
    
    
    /** 调用单例 */
    //    [[KitchenManager defaultManager] GetKitchenInfo:nil];
    
    [[KitchenManager defaultManager] GetKitchenInfoComplete:^{
        [self reloadInputViews];
    }];
    
    [self setRefresh];
    
    [self loadMoreData];
    // Do any additional setup after loading the view.
}

/** 下拉刷新 */
-(void)setRefresh{
    __weak __typeof(self) weakSelf = self;
    self.collctionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        NSLog(@"下拉刷新");
        [[KitchenManager defaultManager] GetKitchenInfoComplete:^{
//            [self.collctionView reloadData];
            
//            // 模拟延迟加载数据，因此2秒后才调用（真实开发中，可以移除这段gcd代码）
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(MJDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakSelf.collctionView reloadData];
            // 结束刷新
            [weakSelf.collctionView.header endRefreshing];
            });
        }];
    }];
    [self.collctionView.header beginRefreshing];
    
}

/** 上拉加载 */
-(void)loadMoreData{
    self.collctionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        __weak __typeof(self) weakSelf = self;
        
        [[KitchenManager defaultManager] GetKitchenInfoComplete:^{
//            [[self.collctionView reloadData];
            
        
        
        //延迟两秒刷新
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(MJDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf.collctionView reloadData];
            
            [weakSelf.collctionView .footer endRefreshing];
            
            
        });
            }];
    }];
    self.collctionView.footer.hidden = YES;
    
}

- (void)viewWillAppear:(BOOL)animated{
    [self.collctionView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/** 生活圈显示条数 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return [KitchenManager defaultManager].XHLifeList.count;
}

/** 生活圈 重用池 */
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    RankCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RankCell" forIndexPath:indexPath];
    
//    RankingKitchenInfo *modelR = [KitchenManager defaultManager].rankingList[indexPath.row];
//    [cell setCellWithRankingKitchenInfo:modelR];
   /** 去除第一张无用照片 */
//    if (indexPath.row == 0) {
//        
//    cell.RankLabel.text = @" ";
//        cell.RankImage.image = [UIImage imageNamed:@"icon"];
//
//    }
        XiangHaLifeInfo *model = [KitchenManager defaultManager].XHLifeList[indexPath.row];
        
        [cell setCellWithXiangHaLifeInfo:model];

    
    
        
    
       return cell;
    
}





/** 点击cell，进入详情页面 */
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    XiangHaLifeInfo *modelXiangHai = [KitchenManager defaultManager].XHLifeList[indexPath.row];
    //    NSLog(@"点击了cell，进入详情页面");
    UIStoryboard* storyborad = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    
    /** 从storyboard中找到对应的ViewController实例 */
    RankDetailViewController* rvc = [storyborad instantiateViewControllerWithIdentifier:@"ChangeTickerAirLineVC"];
//    RankDetailViewController *rvc  = [[RankDetailViewController alloc]init];
    
    /** 代码赋值 */
    rvc.i = modelXiangHai.title;
    rvc.j = modelXiangHai.content;
    rvc.k = modelXiangHai.imgs;
    /**
//    rvc.k = modelXiangHai.imgShow;
//    rvc.TittleLabel.text = modelXiangHai.title;
//    
//    rvc.TextLabels.text = modelXiangHai.content;
//    rvc.imageDetail.image = [UIImage imageNamed:[modelXiangHai.imgs firstObject]];
  
//    NSLog(@"======%@",rvc.Text);
//    NSLog(@"11111%@",modelXiangHai.title);
//    NSLog(@"%@",rvc.TittleLabel.text);
    */
//    [self.navigationController pushViewController:rvc animated:YES];
    
    [self showViewController:rvc sender:nil];
}


/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
